﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace UFIDA.U8.YYNBJY.DBHelp
{
    
    public class LogException
    {
        public static bool ilog =Convert.ToBoolean(ConfigurationManager.AppSettings["ilog"].ToString());
        /// <summary>
        /// 将异常打印到LOG文件
        /// </summary>
        /// <param name="ex">异常</param>
        /// <param name="LogAddress">日志文件地址</param>
        public static void WriteiLog(string filename,string srs,bool ilog,bool iilog)
        {
            if (ilog||iilog)
            {
                if (string.IsNullOrEmpty(filename))
                {
                    filename = "异常";
                }
                string logPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"\log\"+DateTime.Now.ToString("yyyy-MM-dd");
                if (!Directory.Exists(logPath))
                    Directory.CreateDirectory(logPath);
                string LogAddress = logPath+@"\"+"["+filename+"]日志.log";
                //把异常信息输出到文件
                StreamWriter fs = new StreamWriter(LogAddress, true);
                fs.WriteLine("当前时间：" + DateTime.Now.ToString());
                fs.WriteLine("当前：" + srs);
                fs.WriteLine();
                fs.Close();
            }
        }


        private static string e = "";

        private static string eg = "";

        private static string sg = "";

        /// <summary>
        /// 将异常打印到LOG文件
        /// </summary>
        /// <param name="ex">异常</param>
        /// <param name="LogAddress">日志文件地址</param>
        public static void WriteLog(Exception ex, string sql)
        {
            string logPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"\log";

            if (!Directory.Exists(logPath))
                Directory.CreateDirectory(logPath);

            string LogAddress = logPath + @"\" + DateTime.Now.ToString("yyyy-MM-dd") + "-异常日志.log";

            //把异常信息输出到文件
            StreamWriter fs = new StreamWriter(LogAddress, true);
            fs.WriteLine("当前时间：" + DateTime.Now.ToString());
            fs.WriteLine("异常信息：" + ex.Message);
            fs.WriteLine("异常对象：" + ex.Source);
            fs.WriteLine("调用堆栈：\n" + ex.StackTrace.Trim());
            fs.WriteLine("触发语句：" + sql);
            fs.WriteLine();
            fs.Close();
            throw ex;
        }

        public static string WriteTxtLog(Exception ex)
        {
            string str = ex.ToString();
            e = str.Substring(0, str.IndexOf('语'));
            return e;
        }

        public static string WriteTxtEog(Exception ex)
        {
            string str = ex.ToString();
            eg = str.Substring(0, str.IndexOf('在'));
            return eg;
        }

        public static string WriteTxtSog(string cw)
        {
            sg = cw;
            return sg;
        }

        /// <summary>
        /// 错误信息
        /// </summary>
        /// <returns></returns>
        public static string exlog()
        {
            string es;
            if (e == "")
            {
                es = eg;
            }
            else
            {
                es = e;
            }
            if (sg != "")
            {
                es = sg;
            }
            return es;
        }

        
        /// <summary>
        /// 将异常打印到LOG文件
        /// </summary>
        /// <param name="ex">异常</param>
        /// <param name="LogAddress">日志文件地址</param>
        public static void WriteLogts(string srs)
        {
            string logPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"\log";
            if (!Directory.Exists(logPath))
                Directory.CreateDirectory(logPath);
            string LogAddress = logPath + @"\" + DateTime.Now.ToString("yyyy-MM-dd") + "-异常日志.log";
            //把异常信息输出到文件
            StreamWriter fs = new StreamWriter(LogAddress, true);
            fs.WriteLine("当前时间：" + DateTime.Now.ToString());
            fs.WriteLine("当前：" + srs);
            fs.WriteLine();
            fs.Close();
        }

        /// <summary>
        /// 将异常打印到LOG文件
        /// </summary>
        /// <param name="ex">异常</param>
        /// <param name="LogAddress">日志文件地址</param>
        public static void WriteLogTZ(Exception ex, List<string> str)
        {
            string logPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"\log";

            if (!Directory.Exists(logPath))
                Directory.CreateDirectory(logPath);

            string LogAddress = logPath + @"\" + DateTime.Now.ToString("yyyy-MM-dd") + "-异常日志.log";

            //把异常信息输出到文件
            StreamWriter fs = new StreamWriter(LogAddress, true);
            fs.WriteLine("当前时间：" + DateTime.Now.ToString());
            fs.WriteLine("异常信息：" + ex.Message);
            fs.WriteLine("异常对象：" + ex.Source);
            fs.WriteLine("调用堆栈：\n" + ex.StackTrace.Trim());
            fs.WriteLine("触发语句：" + str.ToString());
            fs.WriteLine();
            fs.Close();
            throw ex;
        }
    }
}
